package lg.minecraft.plugin.testeconomy.command;

import lg.minecraft.plugin.testeconomy.config.MessageConfig;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;

/**
 * @author langle__
 * @version 1.0
 */
public class ManiCommand implements TabExecutor {

    private final Map<String, SubCommand> subCommands = new HashMap<>();

    {
        this.registerSubCommand(new HelpCommand());
        this.registerSubCommand(new GiveCommand());
        this.registerSubCommand(new TakeCommand());
        this.registerSubCommand(new SetCommand());
        this.registerSubCommand(new BalanceCommand());
        this.registerSubCommand(new ReloadCommand());
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
        if (args.length == 0) {
            for (String info : MessageConfig.getPLUGIN_INFO_ARRAY()) {
                sender.sendMessage(info);
            }
            return true;
        }

        SubCommand subCommand = this.subCommands.get(args[0]);
        if (subCommand != null) {
            String[] temp = Arrays.copyOfRange(args, 1, args.length);
            subCommand.onCommand(sender, temp);
            return true;
        }

        sender.sendMessage(MessageConfig.getUNKNOWN_COMMAND());
        return false;
    }

    @Nullable
    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
        if (args.length == 1) {
            List<String> list = new ArrayList<>();
            subCommands.values().forEach(subCommand -> list.add(subCommand.getName()));
            return list;
        }
        if (args.length == 2) {
            List<String> list = new ArrayList<>();
            Bukkit.getOnlinePlayers().forEach(player -> list.add(player.getName()));
            return list;
        }
        if (args.length == 3) {
            return Arrays.asList("100", "1000", "10000", "100000");
        }
        return Collections.emptyList();
    }

    public void registerSubCommand(SubCommand subCommand) {
        this.subCommands.put(subCommand.getName().toLowerCase(), subCommand);
    }

}
